package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.*;
import com.sky.entity.Employee;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.*;
import com.sky.mapper.DishMapper;
import com.sky.mapper.EmployeeMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.yaml.snakeyaml.events.Event;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 新增套餐
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //创建套餐对象用于数据库新增操作
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入1条数据
        setmealMapper.save(setmeal);
        Long setmealId = setmeal.getId();
        //向套餐和菜品的关联表插入一条数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() != 0){
            setmealDishes.forEach(setmealDish->{
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.save(setmealDishes);
        }
    }

    /**
     * 分页查询
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 修改套餐
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐表数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //删除套餐中的菜品
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        if (setmealDishes != null && setmealDishes.size() != 0){
            //添加套餐中的菜品
            setmealDishes.forEach(setmealDish->{
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.save(setmealDishes);
        }
    }

    /**
     * 根据id查询套餐
     */
    @Transactional
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        if (setmealDishes != null && setmealDishes.size() != 0){
            setmealVO.setSetmealDishes(setmealDishes);
        }
        return setmealVO;
    }

    /**
     * 批量删除套餐
     */
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //起售中的套餐不可以删除
        List<Integer> status = setmealMapper.getStatusByIds(ids);
        if (status.contains(StatusConstant.ENABLE)){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //根据套餐id删除套餐
        setmealMapper.deleteByIds(ids);
        //根据套餐id删除与套餐的关联数据
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 套餐起售、停售
     */
    @Transactional
    public void changeStatus(Integer status, Long id) {
        //如果套餐中有停售的菜品，不允许起售套餐
        if (status == StatusConstant.ENABLE){
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
            List<Long> dishIds = new ArrayList<>();
            for (SetmealDish setmealDish : setmealDishes) {
                dishIds.add(setmealDish.getDishId());
            }
            List<Integer> statusList = dishMapper.getStatusByIds(dishIds);
            if(statusList.contains(StatusConstant.DISABLE)){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
